import React, { Component } from 'react';
import { StyleSheet, Text, View, PixelRatio, Image, FlatList, BackHandler } from "react-native";
import { API, API_GENERATE } from '../../../config/api';
import { GLOBAL } from '../../../config/global';
import { style } from '../../../theme/style';
import { Common } from '../../../component/common/Common.js';
import { PageStatusIndex } from '../../../component/page-status/PageStatusIndex';
import HTTP from '../../../utils/http';

const scale = GLOBAL.SCALE;

export class HistoryPricePage extends Component {
	static navigationOptions = ({ navigation, navigationOptions }) => {
		return {
			headerTitle: '提报历史',
			headerTintColor: style.color.white,
			headerLeft: <Common type="headLeft" onPress={() => { navigation.goBack() }} color="#fff" />,
			headerRight: (<Image style={{ marginLeft: 18 * scale, width: 9 * scale, height: 16 * scale }} source={require('../../../imgs/goBackEmpty.png')} />),
			headerStyle: style.head.headerStyle,
			headerTitleStyle: style.head.headerTitleStyle
		}
	};

	constructor(props) {
		super(props);
		this.state = {
			recordCount: 0,   // 数据总条数
			nowList: 0,  // 当前条数
			newLotData: [],  // 待审批
			status: 'loading',
			loadIcon: false,  // 下拉涮新的 图标
			selectIndex: '',
			applyIdChecked: {},
		}
		this.pageNo = 1;
		this.totalPage = 1;
		this.goodsId = props.navigation.state.params.goodsId;//商品编号
	}

	componentWillMount () {
		this.props.navigation.setParams({
			title: '提报历史'
		});
		this._getListData();
		BackHandler.addEventListener('hardwareBackPress', this._onBackAndroid);
	}

	componentWillUnmount () {
		this.state = {};
	}

	_onBackAndroid = () => {
		this.props.navigation.navigate('HomePage');
		return true;
	};

	toggleUserIdChecked (applyId) {
		const { applyIdChecked } = this.state;
		const val = applyIdChecked[applyId] ? 0 : 1;
		applyIdChecked[applyId] = val;
		this.setState({
			applyIdChecked
		});
	}


	//获取待审批,已审批列表
	async _getListData (agentObj) {
		let sendData = {
			onrefresh: false,
			pageNo: 1,
			goodsId: this.goodsId,
			//prodDate: '20181201',
		}
		Object.assign(sendData, agentObj);
		this.setState({
			loadIcon: true
		})
		//ORDER_LIST/PRICE_LOT_HIS
		let responseData = await HTTP.POST(API_GENERATE(API.PRICE_HISTORY), sendData);
		this.setState({
			loadIcon: false
		});
		//responseData = [1, 2];

		if (responseData) {
			this.totalPage = responseData.totalPage;
			if (sendData.onrefresh) {  // 加载
				this.setState({
					newLotData: this.state.newLotData.concat(responseData.rows),
					recordCount: responseData.recordCount,
					nowList: this.state.nowList + responseData.rows.length,
					status: 'data'
				})
			} else { // 刷新
				this.setState({
					newLotData: responseData.rows,
					status: responseData.rows.length ? 'data' : 'empty',
					recordCount: responseData.recordCount,
					nowList: responseData.rows.length
				})
			}
		} else {
			this.setState({
				status: 'error'
			})
		}
	}

	// 下拉更新函数
	_isLoadRefresh () {
		this._getListData();
	}

	//上啦加载
	_loadData () {
		if (this.totalPage > this.pageNo) {
			this.pageNo++;
			this._getListData({
				pageNo: this.pageNo,
				onrefresh: true
			});
		}
	}

	//数据加载出错回调
	_onErrorPress = () => {
		this.setState({
			status: 'loading'
		}, function () {
			this._getListData();
		})
	}

	simpleFormatAsMoney (mnt) {
		// mnt -= 0;
		mnt = mnt ? (Math.round(mnt * 100)) / 100 : undefined;
		return (mnt == Math.floor(mnt)) ? mnt + '.00' : ((mnt * 10 == Math.floor(mnt * 10)) ? mnt + '0' : mnt);
	}

	_changeTab = (type) => {
		totalPage = 0;
		pageNo = 1;
		this.setState({
			tabBotName: type,
			status: 'loading'
		}, function () {
			this._getListData();
		})
		/*this.setState({
			listType: type,
			dataSource: [],
		})
		this._fetchCustomerAsync(1, type);*/
	}

	_renderItem (data) {
		console.log('data', data);
		return (
			<View style={{ backgroundColor: '#fff', marginBottom: 12 * scale }}>
				<View style={[styles.topView]}>
					<Text style={[styles.topText]}> {data.item.createTime}</Text>
				</View>
				<View style={{
					flexDirection: 'row',
					marginLeft: 12 * scale,
					marginRight: 12 * scale,
				}}>
					<View style={[styles.leftView]}>
						<View style={{ flexDirection: 'row' }}><Text style={[styles.nameText]}>{data.item.bizStaffName}</Text><Text style={[styles.priceText]}>￥{data.item.suggestedPrice || 0}</Text></View>
						<Text style={[styles.goodsText]} numberOfLines={1} ellipsizeMode='tail'>{data.item.goodsName}</Text>
					</View>
					<View style={[styles.rightView]}>
						<Text style={[styles.statusText]}>{data.item.statusName}</Text>
						<Text style={[styles.departText]}>{data.item.taskName}</Text>
					</View>
				</View>
			</View >
		)

	}

	render () {
		let { recordCount, nowList, tabIndex, companyName, createTime, status, selectCallBack, newLotData, loadIcon } = this.state;
		return (
			<View style={styles.container}>
				{
					status == 'data' ? (
						<View style={styles.listBox}>
							<FlatList
								data={newLotData}
								renderItem={(data) => this._renderItem(data)}
								refreshing={loadIcon}
								onRefresh={this._isLoadRefresh.bind(this)}
								onEndReachedThreshold={1}
								onEndReached={() => { this._loadData() }}
							/>

						</View>

					) : null
				}
				<PageStatusIndex status={status} emptyText="暂无记录" onErrorPress={this._onErrorPress} />
			</View >
		)
	}
}
const styles = StyleSheet.create({
	container: {

		flex: 1,
		backgroundColor: style.color.bgGray,
	},
	listBox: {

	}, topView: {
		height: 39 * scale,
		marginLeft: 12 * scale,
		marginRight: 12 * scale,
		borderBottomWidth: 1 / PixelRatio.get(),
		borderStyle: 'solid',
		borderBottomColor: '#E4E4E4',
	}, topText: {
		marginTop: 11 * scale,
		fontSize: 12 * scale,
	}, nameText: {
		fontSize: 15 * scale,
	}, priceText: {
		fontSize: 15 * scale,
		color: '#2F7BFF',
	}, goodsText: {
		width: 240 * scale,
		marginTop: 8 * scale,
		fontSize: 12 * scale,
		color: '#8B8B8B',
	},
	leftView: {
		width: 250 * scale, marginTop: 10 * scale,
		marginBottom: 13 * scale,
		borderRightWidth: 1 / PixelRatio.get(),
		borderStyle: 'solid',
		borderRightColor: '#E4E4E4',
	}, rightView: {
		marginTop: 10 * scale,
		marginLeft: 23 * scale,
	}, statusText: {
		fontSize: 15 * scale,
		color: '#FC5454',
	}, departText: {
		fontSize: 12 * scale,
		color: '#8B8B8B',
		marginTop: 8 * scale,
	}
})